""" -*- mode: python; coding: utf-8; -*-

    Copyright 2008 Asier Aguirre <asier.aguirre@gmail.com>
    This file is part of Intromit.

    Intromit is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Intromit is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Intromit.  If not, see <http://www.gnu.org/licenses/>.
"""

import sys, python_config, pyintromit, unittest, pickle, python_runner
pyintromit.load("intromit/test/.libs/libpython_members.so")
import libpython_members


class test_2_members(unittest.TestCase):

    def setUp(self):
        self.uut=libpython_members.python_members()

    def assertExpression(self, exception, expression):
        try:
            exec(expression)
            self.fail("Expression did not failed: "+expression)
        except exception:
            pass

    def test_01(self):
        "Test constructor initialization value for: vector"
        self.assertEqual(len(self.uut.sample_vector), 2, "Vector initial value")

    def test_02(self):
        "Test constructor initialization value for: int"
        self.assertEqual(self.uut.sample_int, -10, "Int initial value")

    def test_03(self):
        "Test constructor initialization value for: unsigned"
        self.assertEqual(self.uut.sample_unsigned, 12, "Unsigned initial value")

    def test_04(self):
        "Test constructor initialization value for: short"
        self.assertEqual(self.uut.sample_short, -14, "Short initial value")

    def test_05(self):
        "Test constructor initialization value for: ushort"
        self.assertEqual(self.uut.sample_ushort, 16, "Ushort initial value")

    def test_06(self):
        "Test constructor initialization value for: char"
        self.assertEqual(self.uut.sample_char, -18, "Char initial value")

    def test_07(self):
        "Test constructor initialization value for: uchar"
        self.assertEqual(self.uut.sample_uchar, 20, "Uchar initial value")

    def test_08(self):
        "Test constructor initialization value for: float"
        self.assertAlmostEqual(self.uut.sample_float, -22.24, 1, "Float initial value")

    def test_09(self):
        "Test constructor initialization value for: double"
        self.assertAlmostEqual(self.uut.sample_double, 26.28, 1, "Double initial value")

    def test_10(self):
        "Test constructor initialization value for: longlong"
        self.assertEqual(self.uut.sample_longlong, -32, "Longlong initial value")

    def test_11(self):
        "Test constructor initialization value for: ulonglong"
        self.assertEqual(self.uut.sample_ulonglong, 34, "Ulonglong initial value")

    def test_12(self):
        "Test value setting and checking again for: vector"
        self.uut.sample_vector=[9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1]
        self.assertEqual(self.uut.sample_vector, [9, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1], "Vector set and checked")
        self.uut.sample_vector=[]
        self.assertEqual(self.uut.sample_vector, [], "Vector empty set and checked")
        self.uut.sample_vector=range(1<<10)
        self.assertEqual(self.uut.sample_vector, range(1<<10), "Vector big set and checked")

    def test_13(self):
        "Test value setting and checking again for: int"
        self.uut.sample_int=-87654321
        self.assertEqual(self.uut.sample_int, -87654321, "Int set and checked")

    def test_14(self):
        "Test value setting and checking again for: unsigned"
        self.uut.sample_unsigned=87654321
        self.assertEqual(self.uut.sample_unsigned, 87654321, "Unsigned set and checked")

    def test_15(self):
        "Test value setting and checking again for: short"
        self.uut.sample_short=-8765
        self.assertEqual(self.uut.sample_short, -8765, "Short set and checked")

    def test_16(self):
        "Test value setting and checking again for: ushort"
        self.uut.sample_ushort=8765
        self.assertEqual(self.uut.sample_ushort, 8765, "Ushort set and checked")

    def test_17(self):
        "Test value setting and checking again for: char"
        self.uut.sample_char=-87
        self.assertEqual(self.uut.sample_char, -87, "Char set and checked")

    def test_18(self):
        "Test value setting and checking again for: uchar"
        self.uut.sample_uchar=87
        self.assertEqual(self.uut.sample_uchar, 87, "Uchar set and checked")

    def test_19(self):
        "Test value setting and checking again for: float"
        self.uut.sample_float=1.23e5
        self.assertAlmostEqual(self.uut.sample_float, 1.23e5, 1, "Float set and checked")

    def test_20(self):
        "Test value setting and checking again for: double"
        self.uut.sample_double=2.34e6
        self.assertAlmostEqual(self.uut.sample_double, 2.34e6, 1, "Double set and checked")

    def test_21(self):
        "Test value setting and checking again for: longlong"
        self.uut.sample_longlong=-1234
        self.assertEqual(self.uut.sample_longlong, -1234, "Longlong set and checked")

    def test_22(self):
        "Test value setting and checking again for: ulonglong"
        self.uut.sample_ulonglong=1234
        self.assertEqual(self.uut.sample_ulonglong, 1234, "Ulonglong set and checked")

    def test_23(self):
        "Check documentation for: vector"
        self.assertEqual(libpython_members.python_members.sample_vector.__doc__,
                         "this is a vector of integers", "Doc vector")

    def test_24(self):
        "Check documentation for: int"
        self.assertEqual(libpython_members.python_members.sample_int.__doc__,
                         "this is an integer", "Doc int")

    def test_25(self):
        "Check documentation for: unsigned"
        self.assertEqual(libpython_members.python_members.sample_unsigned.__doc__,
                         "this is an unsigned", "Doc unsigned")

    def test_26(self):
        "Check documentation for: short"
        self.assertEqual(libpython_members.python_members.sample_short.__doc__,
                         "this is a short", "Doc short")

    def test_27(self):
        "Check documentation for: ushort"
        self.assertEqual(libpython_members.python_members.sample_ushort.__doc__,
                         "this is an ushort", "Doc ushort")

    def test_28(self):
        "Check documentation for: char"
        self.assertEqual(libpython_members.python_members.sample_char.__doc__,
                         "this is a char", "Doc char")

    def test_29(self):
        "Check documentation for: uchar"
        self.assertEqual(libpython_members.python_members.sample_uchar.__doc__,
                         "this is an uchar", "Doc uchar")

    def test_30(self):
        "Check documentation for: float"
        self.assertEqual(libpython_members.python_members.sample_float.__doc__,
                         "this is a float", "Doc float")

    def test_31(self):
        "Check documentation for: double"
        self.assertEqual(libpython_members.python_members.sample_double.__doc__,
                         "this is a double", "Doc double")

    def test_32(self):
        "Check documentation for: longlong"
        self.assertEqual(libpython_members.python_members.sample_longlong.__doc__,
                         "this is a longlong", "Doc longlong")

    def test_33(self):
        "Check documentation for: ulonglong"
        self.assertEqual(libpython_members.python_members.sample_ulonglong.__doc__,
                         "this is an ulonglong", "Doc ulonglong")

    def test_34(self):
        "Check documentation for: class"
        self.assertEqual(libpython_members.python_members.__doc__,
                         "python_members class", "Doc class")

    def test_35(self):
        "Check C/python limits for: int"
        self.uut.sample_int=2147483647
        self.assertEqual(self.uut.sample_int, 2147483647, "Int > limit")
        self.uut.sample_int=-2147483648
        self.assertEqual(self.uut.sample_int, -2147483648, "Int < limit")
        self.assertExpression(OverflowError, "self.uut.sample_int=2147483648L")
        self.assertExpression(OverflowError, "self.uut.sample_int=-2147483649L")

    def test_36(self):
        "Check C/python limits for: unsigned"
        self.uut.sample_unsigned=4294967295L
        self.assertEqual(self.uut.sample_unsigned, 4294967295L, "Unsigned > limit")
        self.assertExpression(OverflowError, "self.uut.sample_unsigned=4294967296L")

    def test_37(self):
        "Check C/python limits for: short"
        self.uut.sample_short=32767
        self.assertEqual(self.uut.sample_short, 32767, "Short > limit")
        self.uut.sample_short=-32768
        self.assertEqual(self.uut.sample_short, -32768, "Short < limit")
        self.assertExpression(OverflowError, "self.uut.sample_short=32768")
        self.assertExpression(OverflowError, "self.uut.sample_short=-32769")

    def test_38(self):
        "Check C/python limits for: ushort"
        self.uut.sample_ushort=65535
        self.assertEqual(self.uut.sample_ushort, 65535, "Ushort > limit")
        self.assertExpression(OverflowError, "self.uut.sample_ushort=65536")

    def test_39(self):
        "Check C/python limits for: char"
        self.uut.sample_char=127
        self.assertEqual(self.uut.sample_char, 127, "Char > limit")
        self.uut.sample_char=-128
        self.assertEqual(self.uut.sample_char, -128, "Char < limit")
        self.assertExpression(OverflowError, "self.uut.sample_char=128")
        self.assertExpression(OverflowError, "self.uut.sample_char=-129")

    def test_40(self):
        "Check C/python limits for: uchar"
        self.uut.sample_uchar=255
        self.assertEqual(self.uut.sample_uchar, 255, "Uchar > limit")
        self.assertExpression(OverflowError, "self.uut.sample_uchar=256")

    def test_41(self):
        "Check C/python limits for: longlong"
        self.uut.sample_longlong=9223372036854775807L
        self.assertEqual(self.uut.sample_longlong, 9223372036854775807L, "Longlong > limit")
        self.uut.sample_longlong=-9223372036854775808L
        self.assertEqual(self.uut.sample_longlong, -9223372036854775808L, "Longlong < limit")
        self.assertExpression(OverflowError, "self.uut.sample_longlong=9223372036854775808L")
        self.assertExpression(OverflowError, "self.uut.sample_longlong=-9223372036854775809L")

    def test_41(self):
        "Check C/python limits for: ulonglong"
        self.uut.sample_ulonglong=18446744073709551615L
        self.assertEqual(self.uut.sample_ulonglong, 18446744073709551615L, "Ulonglong > limit")
        self.assertExpression(OverflowError, "self.uut.sample_ulonglong=18446744073709551616L")

    def test_42(self):
        "Check C/python limits for: vector int"
        self.uut.sample_vector=[2147483647, -2147483648]
        self.assertEqual(self.uut.sample_vector, [2147483647, -2147483648], "Vector int <> limit")
        self.assertExpression(OverflowError, "self.uut.sample_vector=[2147483648L]")
        self.assertExpression(OverflowError, "self.uut.sample_vector=[-2147483649L]")

    def test_43(self):
        "Check conversion from float <-> int"
        self.uut.sample_int=1.9
        self.assertEqual(self.uut.sample_int, 1, "Conversion float <-> int")
        self.uut.sample_int=-1.9
        self.assertEqual(self.uut.sample_int, -1, "Conversion float <-> int")

    def test_44(self):
        "Serialization of just constructed object"
        uut2=pickle.loads(pickle.dumps(self.uut))
        self.assertNotEqual(self.uut, uut2, "Unpickled class is the same as pickled one")
        self.assertEqual(self.uut.__getstate__(), uut2.__getstate__(),
                         "Different object contents:\n"+str(self.uut.__getstate__())+"\n"+str(uut2.__getstate__()))

    def test_45(self):
        "Serialization of modified object"
        self.uut.sample_vector=[-1, 0, 1, 99]
        self.uut.sample_int=-1001
        self.uut.sample_unsigned=1002
        self.uut.sample_short=-101
        self.uut.sample_ushort=102
        self.uut.sample_char=-11
        self.uut.sample_uchar=12
        self.uut.sample_longlong=-10001
        self.uut.sample_ulonglong=10002
        self.uut.sample_float=-123.456
        self.uut.sample_double=-456.7890
        self.uut.sample_charp="modified charp"
        self.uut.sample_const_charp="modified const charp"
        uut2=pickle.loads(pickle.dumps(self.uut))
        self.assertEqual(self.uut.__getstate__(), uut2.__getstate__(), "Different object contents")

    def test_46(self):
        "Test constructor initialization value for: char*"
        self.assertEqual(self.uut.sample_charp, "sample string", "Char* initial value")

    def test_47(self):
        "Test constructor initialization value for: const char*"
        self.assertEqual(self.uut.sample_const_charp, "sample const string", "Const char * initial value")

    def test_48(self):
        "Test constructor initialization value for: char[]"
        self.assertEqual(self.uut.sample_char_array, "sample char array", "Char [] initial value")

    def test_49(self):
        "Test constructor initialization value for: double[]"
        self.assertEqual(self.uut.sample_double_array, [0.2, 0.6, 0.1], "Double [] initial value")

    def test_50(self):
        "Test value setting and checking again for: char *"
        self.uut.sample_charp="setting test"
        self.assertEqual(self.uut.sample_charp, "setting test", "Char* setting")

    def test_51(self):
        "Test value setting and checking again for: const char *"
        self.uut.sample_const_charp="setting const test"
        self.assertEqual(self.uut.sample_const_charp, "setting const test", "Const char* setting")

    def test_52(self):
        "Test value setting and checking again for: char []"
        self.uut.sample_char_array="setting char []"
        self.assertEqual(self.uut.sample_char_array, "setting char []", "Char [] setting")
        self.uut.sample_char_array=None
        self.assertEqual(self.uut.sample_char_array, "", "Char [] setting: None")

    def test_53(self):
        "Test too long value setting and checking again for: char []"
        str1="01234567890123456789012345678901234567890123456"
        self.uut.sample_char_array=str1
        self.assertEqual(self.uut.sample_char_array, str1, "Long char [] setting")
        str2="012345678901234567890123456789012345678901234567"
        self.uut.sample_char_array=str2
        self.assertEqual(self.uut.sample_char_array, str1, "Long char [] setting trim")

    def test_54(self):
        "Test bad value setting for: char []"
        self.assertExpression(TypeError, "self.uut.sample_char_array=0")
        self.assertExpression(TypeError, "self.uut.sample_char_array=[]")

    def test_55(self):
        "Test value setting and checking again for: double []"
        self.uut.sample_double_array=[-1, -2, -3]
        self.assertEqual(self.uut.sample_double_array, [-1, -2, -3], "Double [] setting")

    def test_56(self):
        "Test bad value setting for: double []"
        self.assertExpression(TypeError, "self.uut.sample_double_array=0.0")
        self.assertExpression(TypeError, "self.uut.sample_double_array=None")
        self.assertExpression(TypeError, "self.uut.sample_double_array=[1, 2]")
        self.assertExpression(TypeError, "self.uut.sample_double_array=[1, 2, 3, 4]")
        self.assertExpression(TypeError, "self.uut.sample_double_array=[1, None, 3]")

    def test_57(self):
        "Check documentation for: char*"
        self.assertEqual(libpython_members.python_members.sample_charp.__doc__,
                         "this is a char *", "Doc char*")

    def test_58(self):
        "Check documentation for: const char*"
        self.assertEqual(libpython_members.python_members.sample_const_charp.__doc__,
                         "this is a const char *", "Doc const char*")

    def test_59(self):
        "Check documentation for: char []"
        self.assertEqual(libpython_members.python_members.sample_char_array.__doc__,
                         "this is a char array", "Doc char []")

    def test_60(self):
        "Check documentation for: double []"
        self.assertEqual(libpython_members.python_members.sample_double_array.__doc__,
                         "this is a double array", "Doc double []")

    def test_61(self):
        "Check static member variable: int"
        self.assertEqual(self.uut.sample_static_int, -987)
        self.uut.sample_static_int=90909;
        self.assertEqual(self.uut.sample_static_int, 90909)
        self.uut.sample_static_int=-987;

    def test_62(self):
        "Test constructor initialization value for: boolean"
        self.assertEqual(self.uut.sample_bool, True, "Booelan initial value")

    def test_63(self):
        "Test value setting and checking again for: boolean"
        self.uut.sample_bool=False
        self.assertEqual(self.uut.sample_bool, False, "Boolean set and checked")

    def test_64(self):
        "Check documentation for: boolean"
        self.assertEqual(libpython_members.python_members.sample_bool.__doc__,
                         "this is a boolean", "Doc boolean")

    def test_65(self):
        "Test item setting, getting and len: vector"
        self.uut.sample_vector=[1, 3, 5, 0, -1]
        self.assertEqual(self.uut.sample_vector[1], 3)
        self.assertEqual(len(self.uut.sample_vector), 5)
        self.uut.sample_vector[2]=-7
        self.assertEqual(self.uut.sample_vector[2], -7)
        self.assertEqual(self.uut.sample_vector, [1, 3, -7, 0, -1])
        self.assert_(self.uut.sample_vector!=[1, 3, 5, 0, -1])
        

if __name__ == '__main__':

    suite = unittest.TestLoader().loadTestsFromTestCase(test_2_members)
    result=python_runner.BasicTestRunner(iterations=len(sys.argv)>1 and sys.argv[1] or 1,
	stream=len(sys.argv)>2 and open(sys.argv[2], "w") or sys.stderr).run(suite)
    sys.exit(not result.wasSuccessful())
